package com.itheima.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpFormDTO;
import com.itheima.pojo.EmpVO;
import com.itheima.pojo.UserQuery;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class EmpController {
    @Autowired
    private EmpService empService;

    @PostMapping("/users")
    public void saveEmp(@RequestBody EmpFormDTO empFormDTO){
        Emp emp = BeanUtil.copyProperties(empFormDTO, Emp.class);
        empService.save(emp);
    }

    @DeleteMapping("/users/{id}")
    public void deleteEmpById(@PathVariable Integer id){
        empService.removeById(id);
    }

    @GetMapping("/users/{id}")
    public EmpVO seleteById(@PathVariable Integer id){
        Emp emp = empService.getById(id);
        EmpVO empVO = BeanUtil.copyProperties(emp, EmpVO.class);
        return empVO;
    }

    @GetMapping("/users")
    public List<EmpVO> seleteByIds(@RequestParam List<Integer> ids){
        List<Emp> list = empService.listByIds(ids);
        List<EmpVO> empVOS = BeanUtil.copyToList(list, EmpVO.class);
        return empVOS;
    }

    @PutMapping("/users/{id}/deduction/{money}")
    public void deductMoney(@PathVariable Integer id,
                            @PathVariable Integer money){
        empService.deductMoney(id,money);
        return;
    }

    @PostMapping("/list")
    public List<EmpVO> queryUsers(@RequestBody UserQuery userQuery){
        List<Emp> list=empService.queryUsers(userQuery.getName(),userQuery.getStatus(), userQuery.getMinBalance(),userQuery.getMaxBalance());
        return BeanUtil.copyToList(list,EmpVO.class);
    }
}
